﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GenericLibrariesToolKit;
using GenericLibrariesToolKit.Enumeration;
using Model;
using BLL.Users;
using BLL.Users.Impl;


namespace BLL.VirtualTeam.Impl
{
    /// <summary>
    /// 团队管理业务接口实现
    /// </summary>
    public class TeamManageBOImpl : TeamManageBO
    {
        TeamBO teamBO = new TeamBOImpl();
        TeamExpertBO teamExpertBO = new TeamExpertBOImpl();
        public bool BuildTeam(Team team, List<string> expertIdList)
        {
            if (team != null)
            {
                //新建团队
                team.TeamId = GenerateGUID.GenerateGUID36();
                teamBO.Add(team);
                //添加团队领导者为团队成员
                TeamExpert teamExpert = new TeamExpert()
                {
                    TeamId = team.TeamId,
                    ExpertId = team.TeamLeader
                };
                teamExpertBO.Add(teamExpert);
            }
            else
            {
                return false;
            }
            //有新成员加入
            if (expertIdList.Count > 0)
            {
                foreach (string expertId in expertIdList)
                {
                    //构建专家团队关系对象
                    TeamExpert teamExpert = new TeamExpert()
                    {
                        TeamId = team.TeamId,
                        ExpertId = expertId
                    };
                    //增加新成员
                    teamExpertBO.Add(teamExpert);
                }
            }
            return true;
        }

        public bool UpdateTeam(Team team, List<string> expertIdList)
        {
            //有新成员加入
            if (team != null)
            {
                //当修改了团队资料时更新团队资料
                teamBO.Update(team);
                //构建删除条件语句
                WhereCondition[] whereConditions = new WhereCondition[] 
                {
                    new WhereCondition(WhereJoinType.NULL,"TeamId",MatchType.EQUAL,team.TeamId)
                };
                //根据团队id，删除除领导者外的所有成员专家
                long resultCount = teamExpertBO.Delete(whereConditions);
                if (expertIdList.Count > 0)
                {
                    foreach (string expertId in expertIdList)
                    {
                        //构建专家团队关系对象
                        TeamExpert teamExpert = new TeamExpert()
                        {
                            TeamId = team.TeamId,
                            ExpertId = expertId
                        };
                        //增加新成员
                        teamExpertBO.Add(teamExpert);
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        //根据团队id查询团队中所有专家姓名
        public List<ExpertName> getExpertNameList(string teamId)
        {
            List<ExpertName> expertNameList = new List<ExpertName>();
            if (teamId != null)
            {
                //根据团队id查询团队专家关系实体集合
                List<TeamExpert> teamExpertList = teamExpertBO.GetEntityList("TeamId", teamId,null);
                foreach (TeamExpert teamExpert in teamExpertList)
                {
                    ExpertNameBO expertNameBO = new ExpertNameBOImpl();
                    //根据团队专家关系实体中的专家id查询专家姓名,并添加至姓名集合
                    expertNameList.Add(expertNameBO.Find(teamExpert.ExpertId));                  
                }
            }
            //返回对应团队id的所有专家姓名列表
            return expertNameList;
        }
    }
}
